package com.lc.hot100.lc25;

import com.lc.utils.*;

public class Main {

}


class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode t = head;
    	int cnt = k;
    	while(cnt > 1) {
    		if (t == null) {
    			return head;
    		}
    		t = t.next;
    		cnt --;
    	}
		if (t == null) {
			return head;
		}
    	ListNode tmp = t.next;
    	t.next = null;
    	head = reverseList(head);
    	ListNode tt = head;
    	while(tt.next != null) {
    		tt = tt.next;
    	}
    	tt.next = reverseKGroup(tmp, k);
    	
    	return head;
    }
    
    public ListNode reverseList(ListNode head) {
        
    	ListNode pre = null;
    	ListNode h1 = head;
    	
    	while(h1 != null) {
        	ListNode t = h1.next;
        	h1.next = pre;
        	pre = h1;
        	h1 = t;
    	}
    	head = pre;
    	return head;

    }
}